<script setup lang="ts">
import { onHide, onLaunch, onShow } from "@dcloudio/uni-app";
import { getCurrentInstance } from "vue";
import config from "@/config";
import { isEmpty } from "@/utils";

const { proxy } = getCurrentInstance(); //this
onLaunch(options => {
    console.log("[App]App Launch", options);
    const { query } = options; // query参数
    const { path } = options; // 页面路径
    // 缓存页面参数
    uni.setStorageSync("currentOption", options);
    // 缓存当前页面路径
    const pathAndQuery = pathByQuery(path, query);
    // 缓存当前路径
    uni.setStorageSync("currentPage", pathAndQuery);
    // uni.setStorageSync("appCode", process.env['APP_CODE'])
    // #ifdef MP
    console.log(uni.getAccountInfoSync());
    config.appid = uni.getAccountInfoSync().miniProgram.appId;
    uni.setStorageSync("appid", config.appid);
    if (uni.getExtConfig) {
        // 获取提交版本号
        uni.getExtConfig({
            success(res) {
                if (res.extConfig.audit_version) {
                    uni.setStorageSync("auditVersion", res.extConfig.audit_version);
                }
            }
        });
    }
    // #endif

    getNavHeight(); //保存自定义导航栏高度
    // #ifdef MP
    // 小程序初始化配置
    checkMPApplyUpdate();
    // #endif
});
onShow(() => {
    console.log("App Show");
});
onHide(() => {
    console.log("App Hide");
});

function getNavHeight() {
    // 获取胶囊高度，配置导航高度
    let CustomBarHeight;
    let StatusBarHeight;
    const systemInfo = uni.getSystemInfoSync();

    CustomBarHeight =
        systemInfo.platform === "android" ? systemInfo.statusBarHeight + 50 : systemInfo.statusBarHeight + 45;
    StatusBarHeight = systemInfo.statusBarHeight;
    uni.setStorageSync("navHeight", CustomBarHeight + "px");
    uni.setStorageSync("statusBarHeight", StatusBarHeight + "px");
}

//检查是否有新版本
function checkMPApplyUpdate() {
    const updateManager = uni.getUpdateManager();
    updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
        const { hasUpdate } = res;
        console.log("[小程序是否有新版本]", hasUpdate);
    });
    updateManager.onUpdateReady(function () {
        uni.showModal({
            title: "更新提示",
            content: "新版本已经准备好，请重启小程序",
            showCancel: false,
            success: function (res) {
                if (res.confirm) {
                    // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                    updateManager.applyUpdate();
                }
            }
        });
    });
    updateManager.onUpdateFailed(function () {
        // 新的版本下载失败
    });
}

function pathByQuery(path, query) {
    let pathQuery = "";
    if (!isEmpty(query)) {
        pathQuery = "?";
    }
    for (let i in query) {
        pathQuery += `&${i}=${query[i]}`;
    }
    return `/${path}${pathQuery}`;
}
</script>
<style lang="scss">
/*每个页面公共css */
@import "./components/vk-uview-ui/index.scss";
/* 引入项目iconfont图标 */
@import "./css/icon.css";
//全局公共样式
@import "./css/common.css";
//修改全局组件样式
@import "./css/global.scss";
// 自定义css
@import "./css/custom.scss";
//animate动画
@import "./css/animate.css";
</style>
